<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable v-model.trim="form.goods_name" @clear="getList('1')">
            <el-button slot="append" icon="el-icon-search" @click="getList('1')"></el-button>
          </el-input>
        </el-col>
        <el-col :span="12"><el-button type="primary" @click="addOne()">添加商品</el-button> </el-col>
      </el-row>
      <el-table :data="tableData" border stripe>
        <!-- 索引 -->
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）" width="95px"> </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="70px"> </el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="150px">
          <template slot-scope="scope">
            {{ scope.row.add_time | dataFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140px">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top-start">
              <el-button type="primary" size="mini" icon="el-icon-edit" @click.native="editOne(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
              <el-button type="danger" size="mini" icon="el-icon-delete" @click.native="removeOne(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[10, 20, 30]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        goods_name: ''
      },
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableData: []
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList(type) {
      if (type == '1') {
        this.pageNum = 1
        this.pageSize = 10
      }
      const data = {
        query: this.form.goods_name,
        pagenum: this.pageNum,
        pagesize: this.pageSize
      }
      this.$http
        .get('/goods', { params: data })
        .then(res => {
          res = res.data
          // console.log(res)
          if (res.meta.status == 200) {
            this.tableData = res.data.goods
            this.total = res.data.total
            //  console.log('userList',this.userList)
          } else {
            this.$message({ type: 'error', message: res.meta.msg })
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    removeOne(row) {
      //删除商品
      const data = {
        id: row.id
      }
      console.log('data', data)
      this.$confirm(`确定删除商品'${row.goods_name}?'`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http
          .delete(`/goods/${row.id}`, data)
          .then(res => {
            res = res.data
            // console.log(res)
            if (res.meta.status == 200) {
              this.getList('1')
              this.$message({ type: 'success', message: '删除成功!' })
            } else {
              this.$message({ type: 'error', message: res.meta.msg })
            }
          })
          .catch(err => {
            console.log(err)
          })
      })
    },
    addOne() {
      //添加商品
      this.$router.push('/goods/add')
    },
    editOne(row) {
      //编辑商品
      this.$router.push({ path: '/goods/add', query: { id: row.goods_id } })
    },
    removeOne(row) {
      this.$confirm(`确定删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http
          .delete(`/goods/${row.goods_id}`)
          .then(res => {
            res = res.data
            if (res.meta.status != 200) {
              //报错就直接返回
              this.$message({ type: 'error', message: res.meta.msg })
              return
            }
            this.getList('1')
            this.$message({ type: 'success', message: res.meta.msg })
          })
          .catch(err => {
            console.log(err)
          })
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getList()
    }
  }
}
</script>

<style scoped></style>
